<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="订单编号" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入订单编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属会员" prop="memberMobile">
        <el-input
          v-model="queryParams.memberMobile"
          placeholder="请输入会员手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="礼品名称" prop="giftName">
        <el-input
          v-model="queryParams.giftName"
          placeholder="请输入礼品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in stateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="核销时间" prop="verifyTime">
        <el-date-picker
          v-model="verifyDateRange"
          :unlink-panels="unlinkPanels"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="发放时间" prop="gmtTime">
        <el-date-picker
          v-model="dateRange"
          :unlink-panels="unlinkPanels"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>

      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="giftList" >
      <el-table-column label="订单编号" align="center" prop="orderNum" />
      <el-table-column label="所属会员" align="center" prop="memberMobile" />
      <el-table-column label="礼品名称" align="center" prop="giftName" />
      <el-table-column label="礼品价值" align="center" prop="giftPrice" />
      <el-table-column label="礼品市场价" align="center" prop="giftMarketPrice" />
      <el-table-column label="状态" align="center" prop="state" :formatter="stateFormat" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="核销时间" align="center" prop="verifyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.verifyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="失效时间" align="center" prop="expiresTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expiresTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发放时间" align="center" prop="gmtTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleOscarSend(scope.row)"
            v-if="scope.row.state == 0 && scope.row.extcode == 'HNGH202306OSCAR_VIRTUAL_TICKET'"
            v-hasPermi="['act:temp-hngh202306oscar:apply']"
          >发放奥斯卡</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleMxtSend(scope.row)"
            v-if="scope.row.state == 0 && scope.row.extcode == 'HNGH202307MXT_VIRTUAL_TICKET'"
            v-hasPermi="['act:temp-hngh202306oscar:apply']"
          >发放马戏团</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
  import { apply,apply202307mxt } from "@/api/act/temp/hngh202306oscar/ticket";
import { listGift, getGift } from "@/api/order/gift";

export default {
  name: "Gift",
  data() {
    return {
      unlinkPanels:true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 订单礼品表格数据
      giftList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态 0 待核销  3 已核销  5 已失效 7 已过期字典
      stateOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberId: undefined,
        memberMobile: undefined,
        orderId: undefined,
        orderNum: undefined,
        stationId: undefined,
        giftName: undefined,
        giftPrice: undefined,
        giftMarketPrice: undefined,
        verifyCode: undefined,
        state: undefined,
        actId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 日期范围
      dateRange: [],
      verifyDateRange: [],
    };
  },
  created() {
    this.getList();
    this.getDicts("order_gift_state").then(response => {
      this.stateOptions = response.data;
    });
  },
  methods: {
    addGmtDateRange(params, dateRange) {
      var search = params;
      search.gmtStartTime = "";
      search.gmtEndTime = "";
      if (null != dateRange && '' != dateRange) {
        search.gmtStartTime = dateRange[0];
        search.gmtEndTime = dateRange[1];
      }
      return search;
    },
    addVerifyDateRange(params, dateRange) {
      var search = params;
      search.verifyStartTime = "";
      search.verifyEndTime = "";
      if (null != dateRange && '' != dateRange) {
        search.verifyStartTime = dateRange[0];
        search.verifyEndTime = dateRange[1];
      }
      return search;
    },
    /** 查询订单礼品列表 */
    getList() {
      this.loading = true;
      this.addGmtDateRange(this.queryParams,this.dateRange);
      this.addVerifyDateRange(this.queryParams,this.verifyDateRange);
      listGift(this.queryParams).then(response => {
        this.giftList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态 0 待核销  3 已核销  5 已失效 7 已过期字典翻译
    stateFormat(row, column) {
      return this.selectDictLabel(this.stateOptions, row.state);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        memberId: undefined,
        orderId: undefined,
        stationId: undefined,
        giftName: undefined,
        giftPrice: undefined,
        giftMarketPrice: undefined,
        verifyTime: undefined,
        expiresTime: undefined,
        verifyCode: undefined,
        state: undefined,
        remark: undefined,
        actId: undefined,
        gmtTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGift(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单礼品";
      });
    },
    /** 修改按钮操作 */
    handleOscarSend(row) {
      apply(row.id).then(response => {
        this.msgSuccess("操作成功");
        this.getList();
      });
    },
    /** 修改按钮操作 */
    handleMxtSend(row) {
      apply202307mxt(row.id).then(response => {
        this.msgSuccess("操作成功");
        this.getList();
      });
    },
  }
};
</script>
